package com.leetcode;

import java.util.Arrays;

public class TestCountingSort {

	public static void main(String[] arg0) {
		byte[] test = new byte[] { 111, 5, -3, -72, 12, 5, 111, 5, -3, -72, 12,
				5, 111, 5, -3, -72, 12, 5, 111, 5, -3, -72, 12, 5, 111, 5, -3,
				-72, 12, 5, 111, 5, -3, -72, 12, 5 };

		int[] count = new int[256];

		for (int i = 0 - 1; ++i <= test.length - 1; count[test[i] + 128]++);

		for (int i = 256, k = test.length; k > 0;) {
			while (count[--i] == 0) // 这里的while不仅起到if...else的作用，还对i进行了操作
				System.out.println("i = " + i);

			byte value = (byte) (i - 128);
			int s = count[i]; // s一定大于0
			System.out.println("i = " + i + ", s = " + s);

			do { // 因为s一定大于0，所以用do...while先赋值，再过滤条件
				test[--k] = value;
			} while (--s > 0);
		}

		System.out.println("byte[] 排序结果  = " + Arrays.toString(test));

	}

}
